package com.wx.leetcode;

import java.util.Arrays;

/**
 * @description: 数组中两元素的最大乘积
 * @author: wx
 * @create: 2020-08-21 17:43
 **/
public class MaxProductTopic {

    public static int maxProduct(int[] nums) {
        Arrays.sort(nums);
        int length = nums.length;
        int i = length - 1;
        int j = length - 2;
        return (nums[i] - 1) * (nums[j] - 1);
    }

    public static int maxProductV2(int[] nums) {
        int a = nums[0];
        int b = nums[1];

        for (int i = 2; i < nums.length; i++) {
             int value =  nums[i];
             if (a > b && value > b){
                b = value;
             } else if(value > a) {
                a = value;
             }
        }

        return (b-1) * (a-1);
    }

    public static void main(String[] agrs){
        System.out.println(maxProduct(new int[]{3,4,5,2}));
        System.out.println(maxProductV2(new int[]{3,4,5,2}));
    }

}
